package data;

import domain.Usuario;
import java.sql.*;
import java.util.List;
import java.util.LinkedList;
import util.UIDGenerator;

public class JDBCUsuarioDAO implements IUsuarioDAO{
	public	void insertUsuario(Usuario user){
		Connection conn = ConnectionManager.getInstance().checkOut();
		System.out.println("Conexion abierta");
		String sql = "INSERT INTO usuarios(usuarioOID, nick, passwrd, nombre, nacionalidad, email) VALUES(?, ?, ?, ?, ?, ?, ?)";
		PreparedStatement stmt = null;

		try {
			stmt = conn.prepareStatement(sql);
			
			stmt.setString(1, UIDGenerator.getInstance().getKey());
			stmt.setString(2, user.getNick());
			stmt.setString(3, user.getPasswrd());
			stmt.setString(4, user.getNombre());
			stmt.setString(5, user.getNacionalidad());
			stmt.setString(6, user.getEmail());
			
			stmt.executeUpdate();
		} catch (SQLException e){
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			ConnectionManager.getInstance().checkIn(conn);
			System.out.println("Conexion cerrada");
			try {
				if (stmt != null){
					stmt.close();
				}
			} catch (SQLException e){
			}
		}
	}
	
	public void deleteUsuario(String usuarioID){
		Connection conn = ConnectionManager.getInstance().checkOut();
		System.out.println("Conexion abierta");
		String sql = "DELETE FROM usuarios WHERE (usuarioOID = ?)";
		PreparedStatement stmt = null;
		
		try{
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, usuarioID);
			stmt.executeUpdate();
		} catch (SQLException e){
			System.err.println("Message: " + e.getMessage());
			System.err.println("SQLState: " + e.getSQLState());
			System.err.println("ErrorCode: " + e.getErrorCode());
		} finally {
			ConnectionManager.getInstance().checkIn(conn);
			System.out.println("Conexion cerrada");
			try {
				if (stmt != null)
					stmt.close();
			} catch (SQLException e) {
			}
		}
	}
	
	public Usuario selectUsuario(String usuarioID){
		Connection conn = ConnectionManager.getInstance().checkOut();
		System.out.println("Conexion abierta");
		PreparedStatement stmt = null;
		ResultSet result = null;
		Usuario user = null;
		String sql = "SELECT * FROM usuarios WHERE (usuarioOID = ?)";
		try {	
			stmt = conn.prepareStatement(sql);
			stmt.setString(1, usuarioID);
			result = stmt.executeQuery();
			
			result.next();
			user = new Usuario();
			user.setUsuarioID(result.getString("usuarioOID"));
			user.setNick(result.getString("nick"));
			user.setPasswrd(result.getString("passwrd"));
			user.setNombre(result.getString("nombre"));
			user.setNacionalidad(result.getString("nacionalidad"));
			user.setEmail(result.getString("email"));

		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			ConnectionManager.getInstance().checkIn(conn);
			System.out.println("Conexion cerrada");
			try {
				if (result != null) {
					result.close();
				}
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
			}
		}
		return user;
	}
	
	public List<Usuario> selectAllUsuarios(){
		Connection conn = ConnectionManager.getInstance().checkOut();
		System.out.println("Conexion abierta");
		PreparedStatement stmt = null;
		ResultSet result = null;
		List<Usuario> listaUsuarios = new LinkedList<Usuario>();
		String sql = "SELECT * FROM usuarios";
		try {
			stmt = conn.prepareStatement(sql);
			result = stmt.executeQuery();
			while (result.next()) {
				Usuario user = new Usuario();
				user.setUsuarioID(result.getString("usuarioOID"));
				user.setNick(result.getString("nick"));
				user.setPasswrd(result.getString("passwrd"));
				user.setNombre(result.getString("nombre"));
				user.setNacionalidad(result.getString("nacionalidad"));
				user.setEmail(result.getString("email"));
				
				listaUsuarios.add(user);
			}
		} catch (SQLException e) {
			System.out.println("Message: " + e.getMessage());
			System.out.println("SQLState: " + e.getSQLState());
			System.out.println("ErrorCode: " + e.getErrorCode());
		} finally {
			ConnectionManager.getInstance().checkIn(conn);
			System.out.println("Conexion cerrada");
			try {
				if (result != null) {
					result.close();
				}
				if (stmt != null) {
					stmt.close();
				}
			} catch (SQLException e) {
			}
		}
		return listaUsuarios;
	}
}